home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_geomview.idb / usr / freeware / lib / geomview / mathematica / OOGL.m.z / OOGL.m
Encoding:
Text File  |  1999-01-26  |  11.0 KB  |  304 lines

  1. (* $Header: /usrb/gcg/ngrap/src/bin/geomutil/math2oogl/RCS/OOGL.m,v 1.13 1994/11/17 22:39:29 mbp Exp $ *)
  2.  
  3. BeginPackage["OOGL`"]
  4.  
  5. (*
  6.  
  7. If you want to be able to use this package from a directory other
  8. than the geomview distribution directory, or if you want to be able to
  9. run it from a remote host, then
  10. EITHER
  11.    ensure that the programs
  12.     "geomview", "math2oogl", "togeomview", and "oogl2rib"
  13.    are in your search path (e.g. copy them to /usr/local/bin),
  14.  
  15. OR
  16.  
  17.   - Change the value of $GeomviewPath below to be the absolute pathname
  18.     of the "geomview" shell script on your system and
  19.   - Change the value of $GeomRoot below to be the absolute pathname
  20.     of the geomview distribution directory on your system.
  21.  
  22. The first alternative is preferred when possible.
  23. *)
  24.  
  25. (* $GeomviewPath = "/usr/local/bin/geomview"; *)
  26. (* $GeomRoot = "/u/gcg/ngrap"; *)
  27. $GeomviewPath = "geomview";
  28. $GeomRoot = ".";
  29.  
  30. (* DO NOT CHANGE ANYTHING BELOW HERE *)
  31.  
  32.  
  33. (*******************************************************************)
  34.  
  35. (* Copyright (c) 1993 The Geometry Center; University of Minnesota
  36.    1300 South Second Street Suite 500;  Minneapolis, MN  55454, USA;
  37.    
  38. This is file OOGL.m.  OOGL.m is free software.  You can redistribute
  39. it and/or modify it only under the terms given in the file COPYING,
  40. which you should have received along with this file.  this and other
  41. related software may be obtained via anonymous ftp from geom.umn.edu;
  42. email: software@geom.umn.edu. *)
  43.  
  44.  
  45. (* Authors: Nils McCarthy (nils@geom.umn.edu) 
  46.         Stuart Levy (slevy@geom.umn.edu)
  47.         Tamara Munzner (munzner@geom.umn.edu) 
  48.         Silvio Levy (levy@geom.umn.edu)
  49. *)
  50.  
  51.  
  52. (* To use this, you also need to install the math2oogl and oogl2rib C programs.
  53.    They actually do the grunt work, as Mathematica is too slow
  54.    to do it in any reasonable amount of time. *)
  55.  
  56. WriteOOGL::usage = "
  57. WriteOOGL[filename,-Graphics3D or SurfaceGraphics-] writes the given
  58. graphics object or list of graphics objects to filename.  Graphics3D
  59. objects are converted to OOGL OFF format, SurfaceGraphics objects are
  60. converted to OOGL MESH format. WriteOOGL understands only Polygon,
  61. Line, and RGBColor Graphics3D directives.  If WriteOOGL encounters any
  62. errors in the Graphics3D object, it silently tries to cope. Depends on
  63. the C program math2oogl.";
  64.  
  65. Geomview::usage= "
  66. Geomview [-Graphics3D or SurfaceGraphics-]\n 
  67. Geomview [\"name\", -Graphics3D or SurfaceGraphics-] writes the given
  68. graphics object or list of graphics objects to geomview, starting a
  69. copy of geomview if necessary.  Graphics3D objects are converted to
  70. OOGL OFF format, SurfaceGraphics objects are converted to OOGL MESH
  71. format.  Polygons, Lines, Points, and RGBColor Graphics3D directives
  72. are understood.  Geomview[\"name\", ...]  creates geomview object with
  73. that name; default is \"Mathematica\".  Depends on the C program
  74. math2oogl.";
  75.  
  76. WriteRIB::usage= "
  77. WriteRIB [filename,-Graphics3D or SurfaceGraphics-]\n 
  78. WriteRIB [filename,-Graphics3D or SurfaceGraphics-, \"options\"]]
  79. writes the given graphics object or list of graphics objects as
  80. RenderMan .rib files. It actually converts the Mathematica graphics
  81. object to the OOGL data format, and then converts the OOGL files to
  82. rib format.  Graphics3D objects become lists of Polygons, and
  83. SurfaceGraphics objects become PatchMeshes. Polygons, Lines, Points,
  84. and RGBColor Graphics3D directives are understood. Depends on the C
  85. programs math2oogl and oogl2rib. WriteRIB[ ..., \"options\"] appends
  86. the given text string to the invocation of oogl2rib.";
  87.  
  88. WriteChunk::usage= "
  89.  
  90. WriteChunk [filename,-Graphics3D or SurfaceGraphics-] is only for use
  91. on machines that cannot start up a remote Unix shell on a machine that
  92. has math2oogl, oogl2rib, and/or geomview installed (i.e. Macs, PCs).
  93. It writes the Mathematica graphics object to a file in a format
  94. accepted by shell scripts which should be run on a machine (Iris or
  95. Next) that has the relevant programs installed. The shell scripts
  96. \"writeoogl\",\"writegv\", and \"writerib\" correspond to WriteOOGL,
  97. Geomview, and WriteRIB respectively: see their usage messages.";
  98.  
  99. DisplayHost::usage= "Option to Geomview[] specifying display hostname
  100. for geomview.  Automatically set to REMOTEHOST or DISPLAY shell
  101. variable if found.  Setting DisplayHost to \"local\" overrides this behavior.\n
  102. To specify a different user name on the remote computer, use\n
  103.     SetOptions[Geomview, DisplayHost -> \"user@host\" ];";
  104.  
  105. GeomviewCommand::usage =
  106. "Option to Geomview[] specifying the command used to start geomview if\n
  107. it is not already running.   May include geomview command-line options.";
  108.  
  109. MachType::usage= "Option to Geomview[] specifying machine type of
  110. DisplayHost.  Should be either \"sgi\" or \"next\".  The default value
  111. is set by Mathematica to be the same as the type of machine on which
  112. Mathematica is running.";
  113.  
  114. (*Geomview /: Options[Geomview] := { DisplayHost -> "" };*)
  115.  
  116. MeshGraphics::usage = "\n
  117. Head for mesh graphics object."
  118.  
  119. Options[Geomview] ^= {DisplayHost->"",
  120.               MachType->If [$System == "NeXT", "next",
  121.                     If [$System == "SPARC", "sun4",
  122.                    "sgi"] ],
  123.               XDisplay -> ":0",
  124.               GeomviewCommand -> $GeomviewPath};
  125.  
  126. Begin["`Private`"]
  127.  
  128. (* Use StringJoin not <> for backward compatibility *)
  129.  
  130. WriteChunk[filename_String, gobj_List] := (
  131.   Map[ WriteChunk[filename,#] &, gobj];                    
  132. );
  133.  
  134. WriteChunk[filename_String, gobj_Graphics3D] := (
  135.   Write[filename, CForm[gobj[[0]]]];
  136.   Write[filename, CForm[N[gobj[[1]]]]];
  137. );
  138.  
  139. GoodHeadQ[gobj_] := (SameQ[gobj[[0]],SurfaceGraphics] ||
  140.      SameQ[gobj[[0]],MeshGraphics] || SameQ[gobj[[0]],BezierPlot`BezierPatch]
  141.     || SameQ[gobj[[0]],BezierPatch])
  142.  
  143. WriteChunk[filename_String, gobj_?GoodHeadQ] := (
  144.   Write[filename, CForm[gobj[[0]]]];
  145.   WriteString[filename, "Dimensions\n"];
  146.   Write[filename, Take[Dimensions[gobj[[1]]],2]];
  147.   If[SameQ[gobj[[0]],SurfaceGraphics],
  148.     WriteString[filename, "MeshRange\n"];
  149.     Write[filename, MeshRange /. gobj[[-1]]]] ;
  150.   (* write out the colors before the points, if they exist *)
  151.   (* Backwards compatibility with v1.2: Dimensions[gobj] returns empty list *)
  152.   If [$VersionNumber >= 2.0,
  153.     If [Dimensions[gobj][[1]] > 2, Write[filename, CForm[gobj[[2]]]]],
  154.     If [Dimensions[Dimensions[gobj[[2]]]][[1]] > 1, 
  155.        Write[filename, CForm[gobj[[2]]]]]];
  156.   Write[filename, CForm[N[gobj[[1]]]]];
  157. );
  158.  
  159. If[$VersionNumber < 2.0, (
  160.     StringTake[str_, take_] := Apply[StringJoin, Take[Characters[str], take]];
  161.     StringPosition[str_, ch_] := Block[{chars = Characters[str]},
  162.     Select[ Table[{i,i}, {i,1,StringLength[str]}], 
  163.         (chars[[ #[[1]] ]] == ch)&] ];
  164.     $Failed = Fail;
  165.   )];
  166.  
  167. BinPrefix[mach_] := StringJoin[ $GeomRoot, "/bin/", mach ];
  168. BinShPrefix[mach_] := StringJoin[ "PATH=$PATH:", BinPrefix[mach], " " ];
  169.  
  170. WriteOut[filename_String, gobj_, postpend_String:"", prepend_String:"",
  171.          options___] :=(
  172.   (* math2oogl expects '{}(), ' to be translated into newlines *)
  173.   tr = "tr -s '{}(), ' '\\12\\12\\12\\12\\12\\12' | ";    
  174.   (* Do the right thing if we've got a command instead of a file name. *)
  175.   tmpfilename =
  176.     If[StringMatchQ[filename, "!*"],
  177.         StringJoin[" | ",StringDrop[filename,1]],
  178.       If[filename != "",
  179.         StringJoin[" > ", filename],
  180.         filename]];
  181.   newfilename =
  182.     StringJoin["!",tr,prepend," ",
  183.            BinShPrefix[ MachType /. {options} /. Options[Geomview]],
  184.            "math2oogl ",postpend, tmpfilename];
  185.   (* We might prepend 'rsh hostname' to the command, which must then be
  186.    * quoted so that what comes after the pipes gets interpreted on
  187.    * the remote not the local machine. The open quote is already in the
  188.    * middle of the prepended string.
  189.    * If we did prepend 'rsh hostname', also add a command to be executed in
  190.    * case the rsh failed, to avoid e.g. mysterious "permission denied" errors.
  191.    * StringMatchQ[prepend,"rsh*"] fails mysteriously in Math 1.2.
  192.    *)
  193.   If[StringLength[prepend]>3 && StringTake[prepend,3] == "rsh",
  194.     newfilename = StringJoin[newfilename,
  195. "'\\' || (echo Geomview graphics: Cannot rsh to machine ", rshhost, " as ",
  196.         If[rshuser != "", StringJoin["user ", rshuser], "yourself"],
  197. ".; echo Check DisplayHost option, or your .rhosts file on ", rshhost, ".)"]];
  198.   OpenWrite[newfilename];    
  199.   WriteChunk[newfilename, gobj];
  200.   Close[newfilename];    
  201.  
  202. );
  203.  
  204. WriteOOGL[filename_String,gobj_] :=(
  205.    WriteOut[filename, gobj];
  206.    gobj
  207. );
  208.  
  209. WriteRIB[filename_String,gobj_, riboptions_String:"", options___] :=(
  210.    WriteOut[filename,gobj, 
  211.       StringJoin[" | oogl2rib -n mma.tiff ",riboptions]];
  212.    gobj
  213. );
  214.  
  215. (*
  216.  * If the given filename is on the $PATH, returns the directory it's in,
  217.  * else returns Null.  Used for load-time warning messages.
  218.  *)
  219. OnPathQ[filename_] :=
  220.     Block[{pdir, i, posen, path = StringJoin[":",Environment["PATH"],":"] },
  221.      (
  222.     posen = StringPosition[path, ":"];
  223.     Do[
  224.       (pdir = StringTake[path, {posen[[i]][[2]]+1, posen[[i+1]][[1]]-1}];
  225.        If[FileType[StringJoin[pdir,"/",filename]] === File,
  226.             Return[pdir]]),
  227.      {i, 1, Length[posen]-1}
  228.     ]
  229.      )];
  230.     
  231.  
  232.  
  233. xhost[display_] := StringTake[display,
  234.    StringPosition[StringJoin[display,":"], ":"] [[1]] [[1]]-1];
  235.  
  236. remotehost[host_String] := (
  237.   newhost="";
  238.   If[host != "",
  239.     newhost = If[host == "local", "", host],
  240.     (
  241.       If[(rshhost = Environment["REMOTEHOST"]) =!= $Failed
  242.      || (rshhost = Environment["DISPLAY"]) =!= $Failed,
  243.       newhost = xhost[rshhost];
  244.       ]
  245.   )];
  246.   If[newhost == $MachineName, newhost = ""];
  247.   If[newhost != "" && Length[StringPosition[newhost, "@"]] == 0 &&
  248.       ((ruser = Environment["REMOTEUSER"]) =!= $Failed
  249.        || (ruser = Environment["USER"]) =!= $Failed
  250.        || (ruser = Environment["LOGNAME"]) =!= $Failed),
  251.     newhost = StringJoin[ruser, "@", newhost]];
  252.   newhost
  253.  );
  254.  
  255. toGV[host_, xdisplay_] :=(
  256.   newhost = remotehost[host];
  257.  
  258.   {rshuser, rshhost} =
  259.     If[Length[atposn = StringPosition[newhost, "@"]] > 0,
  260.      {StringTake[newhost, {1,atposn[[1]][[1]]-1}],
  261.     StringTake[newhost, {atposn[[1]][[2]]+1,StringLength[newhost]}]},
  262.      {"", newhost}];
  263.  
  264.   (* Return our rsh command if any, else the empty string. *)
  265.   If[newhost != "",
  266.      StringJoin["rsh ", rshhost, 
  267.         If[StringLength[rshuser] > 0, " -l ", ""], rshuser,
  268.         " /bin/sh -c \\''DISPLAY=", xdisplay, "; export DISPLAY; "],
  269.      ""
  270.   ]
  271. );
  272.  
  273. Geomview[ name_String, gobj_, options___] := (
  274.    WriteOut["", gobj, StringJoin[" -togeomview ",name," ",
  275.     GeomviewCommand /. {options} /. Options[Geomview]], 
  276.          toGV[DisplayHost /. {options} /. Options[Geomview],
  277.           XDisplay /. {options} /. Options[Geomview]],
  278.       options];
  279.    gobj
  280. );
  281.  
  282. Geomview[ gobj_, options___] := (
  283.   Geomview[ "Mathematica", gobj, options ]; gobj
  284. );
  285.  
  286. bpref = BinPrefix[MachType /. Options[Geomview]];
  287.  
  288. If[Not[StringQ[OnPathQ["math2oogl"]]],
  289.   (Print["Warning: the program 'math2oogl' is not on your search path,\n",
  290.     Environment["PATH"], "\n"];
  291.    If[ FileType[StringJoin[bpref, "/math2oogl"]]===File,
  292.       Print["using default path (in OOGL.m) of ", bpref, " instead."],
  293.     Print[
  294. "the Mathematica->geomview graphics package won't be able to work;\n
  295. change the value of $Geomroot in OOGL.m, or ensure that the math2oogl programs\n
  296. are installed somewhere on your search path."]
  297.     ])
  298. ];
  299.  
  300. End[]
  301. EndPackage[]
  302.  
  303. SetOptions[Geomview, DisplayHost -> OOGL`Private`remotehost[""] ];
  304.